package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalInfo;
import com.moleq.models.RoundingSetting;

public class RoundingSettingService {
	private DatabaseHelper DBHelper;
	private Context mContext;
	private String errorString;

	public RoundingSettingService() {

	}

	public RoundingSettingService(Context context) {
		// this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(
				GlobalInfo.getInstance().ShareContext);
	}

	public List<RoundingSetting> FindAll() {
		List<RoundingSetting> obj = new ArrayList<RoundingSetting>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.RoundingSetting.SCHEMA_NAME, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int RS_ID = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_ID));
			String RS_NAME = cursor.getString(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_NAME));
			double RS_DOWN_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_DOWN_VALUE));
			double RS_UP_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_UP_VALUE));
			double RS_TO_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_TO_VALUE));
			String RS_OPTIONAL = cursor.getString(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_OPTIONAL));
			RoundingSetting rs = new RoundingSetting(RS_ID, RS_NAME, RS_DOWN_VALUE, RS_UP_VALUE, RS_TO_VALUE, RS_OPTIONAL);
			rs.setType("O");
			obj.add(rs);
		}
		cursor.close();
		return obj;
	}
	
	public RoundingSetting FindByID(int no){
		RoundingSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.RoundingSetting.SCHEMA_NAME + " where rs_id = " + no, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int RS_ID = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_ID));
			String RS_NAME = cursor.getString(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_NAME));
			double RS_DOWN_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_DOWN_VALUE));
			double RS_UP_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_UP_VALUE));
			double RS_TO_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_TO_VALUE));
			String RS_OPTIONAL = cursor.getString(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_OPTIONAL));
			obj = new RoundingSetting(RS_ID, RS_NAME, RS_DOWN_VALUE, RS_UP_VALUE, RS_TO_VALUE, RS_OPTIONAL);
		}
		cursor.close();
		return obj;
	}
	
	public RoundingSetting FindByName(String name){
		RoundingSetting obj = null;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.RoundingSetting.SCHEMA_NAME + " where rs_name = '" + name + "'", null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int RS_ID = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_ID));
			String RS_NAME = cursor.getString(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_NAME));
			double RS_DOWN_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_DOWN_VALUE));
			double RS_UP_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_UP_VALUE));
			double RS_TO_VALUE = cursor.getDouble(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_TO_VALUE));
			String RS_OPTIONAL = cursor.getString(cursor.getColumnIndex(DatabaseSchema.RoundingSetting.RS_OPTIONAL));
			obj = new RoundingSetting(RS_ID, RS_NAME, RS_DOWN_VALUE, RS_UP_VALUE, RS_TO_VALUE, RS_OPTIONAL);
		}
		cursor.close();
		return obj;
	}
	
	public boolean CheckRounding(int round_id) {
		boolean lb_check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select rs_id from "+ DatabaseSchema.RoundingSetting.SCHEMA_NAME +" where rs_id  = " + round_id;
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null) {
			MessageBox.show(mContext, DBError.error);
			DBError.error = "";
			return true;
		}
		if (cursor.moveToNext()) {
			lb_check = false;
		}
		cursor.close();
		return lb_check;
	}

	public boolean Insert(RoundingSetting obj) {
		boolean check = true;
		// SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "INSERT INTO " + DatabaseSchema.RoundingSetting.SCHEMA_NAME + "("
				+ DatabaseSchema.RoundingSetting.RS_NAME + ","
				+ DatabaseSchema.RoundingSetting.RS_DOWN_VALUE + ","
				+ DatabaseSchema.RoundingSetting.RS_UP_VALUE + ","
				+ DatabaseSchema.RoundingSetting.RS_TO_VALUE + "," 
				+ DatabaseSchema.RoundingSetting.RS_OPTIONAL + ") values ('"
				+ obj.getRs_name() + "'," + obj.getRs_down_value() + "," + 
				obj.getRs_up_value() + "," + obj.getRs_to_value() + ",'"+ obj.getRs_optional() +"')";
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}

	public boolean Update(RoundingSetting obj) {
		boolean check = true;
		// SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Update " + DatabaseSchema.RoundingSetting.SCHEMA_NAME + " set "
				+ DatabaseSchema.RoundingSetting.RS_NAME + " = '"
				+ obj.getRs_name() + "', "
				+ DatabaseSchema.RoundingSetting.RS_DOWN_VALUE + " = " + obj.getRs_down_value() + ","
				+ DatabaseSchema.RoundingSetting.RS_UP_VALUE + " = " + obj.getRs_up_value() + ","
				+ DatabaseSchema.RoundingSetting.RS_TO_VALUE + " = " + obj.getRs_to_value() + ","
				+ DatabaseSchema.RoundingSetting.RS_OPTIONAL + " = '" + obj.getRs_optional() + "'"
				+ " where " +  DatabaseSchema.RoundingSetting.RS_ID + " = " + obj.getRs_id();
		errorString = DBHelper.execSQL(sql);

		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean Delete(int rs_id) {
		boolean check = true;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "Delete from " + DatabaseSchema.RoundingSetting.SCHEMA_NAME;
		String ls_where = " where " +  DatabaseSchema.RoundingSetting.RS_ID + " = " + rs_id;
		sql = sql + ls_where;
		try {
			db.execSQL(sql);
		} catch (Exception e) {
			check = false;
		}
		return check;
	}
	
	public int getMaxRoundNo(){
		int order = 0;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select Max("+ DatabaseSchema.RoundingSetting.RS_ID +") from " + DatabaseSchema.RoundingSetting.SCHEMA_NAME, null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return order;
		}
		if (cursor.moveToNext()) {
			order = cursor.getInt(0);
		}
		cursor.close();
		return order;
	}
}
